package com.titan.core.utils.db;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.titan.core.utils.object.ListMap;

/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月14日
 */
public class TableMeta {
    private ListMap<String,FieldMeta> fields = new ListMap<String,FieldMeta>();
    private final static Map<Integer,String> SQL_TYPE = new HashMap<Integer,String>();
    static {
//        SQL_TYPE.put(java.sql.Types.NULL, "NULL");   // int NULL            =   0;
//        SQL_TYPE.put(java.sql.Types.CHAR, "CHAR");   // int CHAR            =   1;
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
//        SQL_TYPE.put(java.sql.Types., "");   // 
        
    }
    
    
    /*
public final static int BIT             =  -7;
public final static int TINYINT         =  -6;
public final static int SMALLINT        =   5;
public final static int INTEGER         =   4;
public final static int BIGINT          =  -5;
public final static int FLOAT           =   6;
public final static int REAL            =   7;
public final static int DOUBLE          =   8;
public final static int NUMERIC         =   2;
public final static int DECIMAL         =   3;
public final static 
public final static int VARCHAR         =  12;
public final static int LONGVARCHAR     =  -1;
public final static int DATE            =  91;
public final static int TIME            =  92;
public final static int TIMESTAMP       =  93;
public final static int BINARY          =  -2;
public final static int VARBINARY       =  -3;
public final static int LONGVARBINARY   =  -4;
public final static 
public final static int OTHER           = 1111;
public final static int JAVA_OBJECT         = 2000;
public final static int DISTINCT            = 2001;
public final static int STRUCT              = 2002;
public final static int ARRAY               = 2003;
public final static int BLOB                = 2004;
public final static int CLOB                = 2005;
public final static int REF                 = 2006;
public final static int DATALINK = 70;
public final static int BOOLEAN = 16;
public final static int ROWID = -8;
public static final int NCHAR = -15;
public static final int NVARCHAR = -9;
public static final int LONGNVARCHAR = -16;
public static final int NCLOB = 2011;
public static final int SQLXML = 2009;
public static final int REF_CURSOR = 2012;
public static final int TIME_WITH_TIMEZONE = 2013;
public static final int TIMESTAMP_WITH_TIMEZONE = 2014;
    */
    
    public TableMeta(ResultSet rest) throws SQLException {
        this(rest.getMetaData());
    }
    public TableMeta(ResultSetMetaData metas) throws SQLException {
        for (int i = 1; i <= metas.getColumnCount(); i++) {
            String field = metas.getColumnName(i);
            int type = metas.getColumnType(i);
            FieldMeta meta = new FieldMeta(field, type);
            fields.put(field, meta);
        }
    }
    
    public String toString() {
        StringBuilder buf = new StringBuilder(1024);
        for (int i = 0; i < size(); i++) {
            buf.append("[").append(i).append("] 字段名=").append(getField(i)).append(";  字段类型=").append(getType(i));
        }
        return buf.toString();
    }
    
    public int size() {
        return fields.size();
    }
    
    public String getField(int index) {
        return fields.getValue(index).getName();
    }
    public int getType(int index) {
        return fields.getValue(index).getType();
    }
    public int getType(String field) {
        return fields.get(field).getType();
    }
    
}

class FieldMeta {
    private String name;
    private int type;    // -9表示NVARCHAR    -1表示LONGVARCHAR
    FieldMeta(String name, int type) {
        this.name = name;
        this.type = type;
    }

    public String getName() {
        return name;
    }
    public int getType() {
        return type;
    }
}